package com.smartstate.dataManagement.service.impl;


import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartstate.dataManagement.dto.PageDTO;
import com.smartstate.dataManagement.entity.Team;
import com.smartstate.dataManagement.mapper.TeamMapper;
import com.smartstate.dataManagement.mapper.UserMapper;
import com.smartstate.dataManagement.service.TeamService;
import com.smartstate.dataManagement.util.UserUtils;
import com.smartstate.dataManagement.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team>  implements TeamService {

    @Autowired
    TeamMapper teamMapper;


    @Autowired
    UserMapper  userMapper;


    @Autowired
    HttpServletRequest request;

    @Override
    public ResultVO<String> addTeam(List<Team> teamList) {
        List<Team> teams = new ArrayList<>();
        List<Team> getList = new ArrayList<>();
        if (teamList.size() > 0) {
            // 数据不存在则新增， 否则更新
            getList=teamMapper.getById(teamList);
            if(getList.size()<1) {
                for(Team team: teamList){
                    team.setId(IdUtil.objectId());
                    teams.add(team);
                }
                if (teamMapper.addTeam(teams) > 0) {
                    return ResultVO.ok(String.valueOf(true));
                }
                return ResultVO.fail("新增失败");

            }else{// 存在数据，更新
                    for(Team team: teamList){
                        team.setUpdateTime(new Date());
                        team.setUpdateUser(UserUtils.getLoginUser().getUsername());
                        teams.add(team);
                    }
                    if(teamMapper.editTeam(teamList)>0){
                        return ResultVO.ok("更新成功");
                    }
                    return  ResultVO.ok("200","更新失败");
                }
        }
        return ResultVO.fail("参数缺失，请求失败");
    }


    @Override
    public List<Team> getTeamByType(Team team) {
        List<Team> teams=new ArrayList<>();
        teams=teamMapper.getTeamByType(team.getTeamType());
        return teams;
    }

    @Override
    public List<Team> getTeam() {
        return teamMapper.getTeam();
    }


    @Override
    public Page<Team> queryAllPageByDTO(PageDTO pageDTO) {
        return null;
    }
}
